package cn.servlet;

import cn.entity.Carts;
import cn.entity.Goods;
import cn.entity.Users;
import cn.service.CartsService;
import cn.service.GoodsService;
import cn.service.impl.CartsServiceImpl;
import cn.service.impl.GoodsServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/carts")
public class CartsServlet extends HttpServlet {
	CartsService cartsService=new CartsServiceImpl();
	GoodsService goodsService=new GoodsServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartsServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		switch(action){
		case "query":
			query(request,response);
			break;
		case "add":
			add(request,response);
			break;
		case "update":
			update(request,response);
			break;
		case "delete":
			delete(request,response);
			break;
		}
	}
	//查询
	protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Users users=(Users)request.getSession().getAttribute("users");
		List<Carts> list=cartsService.find(users.getId());
		Double total=0.0;
		for(Carts v:list){
			if(v.getGid()!=null){
				Goods goods=goodsService.selectByPrimaryKey(v.getGid());
				v.setGoods(goods);
			}
			total+=v.getPrice();
		}
		request.setAttribute("list", list);
		request.setAttribute("total", total);
		request.getRequestDispatcher("carts.jsp").forward(request, response);
	}
	
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String sgid=request.getParameter("gid");
		String squantity=request.getParameter("quantity");
		Users users=(Users)request.getSession().getAttribute("users");
		int quantity=0;
		if(!StringUtils.isNullOrEmpty(squantity)){
			quantity=Integer.parseInt(squantity);
		}
		if(!StringUtils.isNullOrEmpty(sgid)){
			Integer gid=Integer.parseInt(sgid);
			Goods goods=goodsService.selectByPrimaryKey(gid);
			Carts carts=new Carts();
			carts.setGid(gid);
			carts.setQuantity(quantity);
			carts.setUid(users.getId());
			carts.setPrice(goods.getPrice()*quantity);
			int n=cartsService.insert(carts);
			JSONObject obj=new JSONObject();
			if(n>0){
				obj.put("code", 200);
				obj.put("msg", "ok");
			}else{
				obj.put("code", 500);
				obj.put("msg", "error");
			}
			out.print(obj.toJSONString());
		}
		out.close();
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String sid=request.getParameter("id");
		String squantity=request.getParameter("quantity");
		int quantity=0;
		if(!StringUtils.isNullOrEmpty(squantity)){
			quantity=Integer.parseInt(squantity);
		}
		if(!StringUtils.isNullOrEmpty(sid)){
			Integer id=Integer.parseInt(sid);
			Carts carts=cartsService.selectByPrimaryKey(id);
			Goods goods=goodsService.selectByPrimaryKey(carts.getGid());
			carts.setQuantity(quantity);
			carts.setPrice(goods.getPrice()*quantity);
			int n=cartsService.updateByPrimaryKey(carts);
			if(n>0){
				out.print("{\"code\":200,\"msg\":\"ok\"}");
			}else{
				out.print("{\"code\":500,\"msg\":\"error\"}");
			}
		}
		out.close();
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String sid=request.getParameter("id");
		
		if(!StringUtils.isNullOrEmpty(sid)){
			Integer id=Integer.parseInt(sid);
			int n=cartsService.deleteByPrimaryKey(id);
			if(n>0){
				out.print("{\"code\":200,\"msg\":\"ok\"}");
			}else{
				out.print("{\"code\":500,\"msg\":\"error\"}");
			}
		}
		out.close();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
}
